package com.zegome.game.gomestory.main.scene.level;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.equations.Sine;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.game.gomestory.main.helper.IMAction;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.gomestory.main.scene.play.PlayingScreen;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.screen.GameScreenOption;
import com.zegome.game.util.helper.WidgetAccessor;

public class LevelScreen extends BaseGameScreen {
	
	public static int GAME_CHAPTER = 0;
	private Table levelTable;
	private Image bg;
	
	public LevelScreen(AndroidGameController controller, String name, final IFacebookBridge bridge) {
		super(controller, name,bridge);		
		setConfig(true, true, true,true);		
	}
	
			
	public void show()
	{
		ctr.a.soundOnOff();
		ctr.a.isHasDialog.removeAllElements();
		ctr.d.backLis.removeAllElements();
		state = 0;
		ctr.d.getLevel();
		stage.clear();
		createUi();
		super.show();
		ctr.a.bgSong.play();
	}
	
	//
	public void createUi()
	{	
		if(bg==null){
			bg = ctr.stageHelper.createImage(ctr.a.bgCommondRegion, 0, 0, (800), (480));
		}
		
		//addIconGomeSmallStage();
		
		stage.addActor(bg);	
		//
		addIconGomeSmallStage();
		//
		levelTable = addLevelTable(GAME_CHAPTER);
		//
		stage.addActor(levelTable);
		//
		Table avatar = ctr.stageHelper.createAvatar(this,stage, tweenManager, (0), (480-99), 1f);
		stage.addActor(avatar);
	}
	@Override
	public void onManagedUpdate(float pDeltaTime) {
		// TODO Auto-generated method stub
		update(pDeltaTime);
		super.onManagedUpdate(pDeltaTime);
	}
	public Table addLevelTable(int chapterIndex)
	{		
		TextureRegion rgTemp = ctr.d.levels[chapterIndex].unlock;		
		TextureRegion rgLockTemp = ctr.d.levels[chapterIndex].lock;				
		//
		float scale = 1.0f;
		final LevelIndex temp = new LevelIndex();
		int deltaX = (int) ((800-108*5*scale)/6);
		int deltaY = (int) ((336-75*4*scale)/5);
		//		
		//
		Table chapter = ctr.stageHelper.createTable(ctr.g.mSkin, 0, (30), (800), (336));
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<5;j++)
			{				
				temp.getActor(ctr,ctr.d.levels[chapterIndex].maxLevel>=i*5+j+1?rgTemp:rgLockTemp, ctr.d.levels[chapterIndex].starCollect[i*5+j], chapterIndex*20+i*5+j+1);
				temp.img.setPosition((j+1)*deltaX+j*(108), (336-(i+1)*76) - (i+1)*deltaY+(20));
				chapter.addActor(temp.img);								
				//
				final int k = chapterIndex*20+i*5+j+1;
				if(ctr.d.levels[chapterIndex].maxLevel>=i*5+j+1||ctr.d.isTestVersion==true){
				temp.img.addListener(new ChangeListener() {
					@Override
					public void changed(ChangeEvent event, Actor actor) {
						if(ctr.a.isHasDialog() == true)
							return;
						if(state != 1)
							return;
						gotoScreen(
							new IMAction() {
								
								@Override
								public void action () {
									// TODO Auto-generated method stub																	
									ctr.a.loadRSGame(k,true);
								}
							},
							new IMAction() {
								
								@Override
								public void action () {
									// TODO Auto-generated method stub
									ctr.a.loadedGame();
									ctr.a.loadedMusic2();
									ctr.a.loadedRSGame(k,true);
								}
							},
							new IMAction() {
								
								@Override
								public void action () {
									// TODO Auto-generated method stub
									PlayingScreen.GAME_LEVEL = k;						
									fadeOut(new TweenCallback() {
										@Override
										public void onEvent(int type, BaseTween<?> source) {
											// TODO Auto-generated method stub		
											ctr.a.bgSong.pause();
											clear();
											ctr.a.clearLevel();
											mController.setScreen(mController
													.getScreen("GameScreen"));
										}
									});		
								}
							});
					}
				});
				}
				//
				temp.img.setPosition((j+1)*deltaX+j*(108)+2*deltaX*(j+1)+(i+1)*deltaX*2, (326-(i+1)*76) - (i+1)*deltaY+(20)-10);
				Tween.to(temp.img, WidgetAccessor.POS_XY, 0.7f).target((j+1)*deltaX+j*(108), (336-(i+1)*76) - (i+1)*deltaY + deltaY+(20)-10).ease(Sine.OUT).start(tweenManager);
			}
		}		
		return chapter;
	}
	
	public void backKeyProcess(){
		//postRunnable(new Runnable() {
			
		//	@Override
		//	public void run() {
				// TODO Auto-generated method stub
				fadeOut(new TweenCallback() {
					@Override
					public void onEvent(int type, BaseTween<?> source) {
						// TODO Auto-generated method stub
						ctr.a.bgSong.pause();
						ctr.a.clearLevel();
						clear();
						ctr.a.loadedChapter();
						mController.setScreen(mController.getScreen("ChapterScreen"));
					}
				});	
		//	}
		//});	
	}	
	public void clear(){
		super.clear();
		levelTable = null;
		bg = null;
	}
}
